#include "gamewidget.h"
#include "mybutton.h"
#include <QGridLayout>


GameWidget::GameWidget(QWidget *parent) : QWidget(parent){

}

GameWidget::GameWidget(int num){
    this->setFixedSize(320, 565);
    this->num = num;

    MyButton * backBtn = new MyButton(":/resource/BackButton.png",":/resource/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(240, 531);
    //返回按钮音效
    QSound * backSound = new QSound(":/resource/BackButtonSound.wav",this);
    connect(backBtn, &MyButton::clicked, [=](){
       backSound->play();
       QTimer::singleShot(160,this,[=](){
            emit sendBackToSelectpane();
       });
    });

    //胜利标语空间
    QPixmap p(":/resource/LevelCompletedDialogBg.png");
    QLabel * lb =new QLabel();
    lb->setPixmap(p);
    lb->setFixedSize(p.width(),p.height());
    lb->setParent(this);
    lb->move(this->width()/2-lb->width()/2,-100);
    animation = new QPropertyAnimation(lb, "geometry");
    animation->setDuration(1000);
    animation->setStartValue(QRect(lb->x(),lb->y(),lb->width(),lb->height()));
    animation->setEndValue(QRect(lb->x(),lb->y()+200,lb->width(),lb->height()));
    animation->setEasingCurve(QEasingCurve::OutBounce);

    // 翻金币音效与胜利音效

    QSound * flipSound = new QSound(":/resource/ConFlipSound.wav", this);
    QSound * winSound = new QSound(":/resource/LevelWinSound.wav", this);


//    初始化关卡数据
    DataConfig data;
    for (int i=0; i<4; i++) {
        for (int j=0; j<4; j++) {
            gameMap[i][j] = data.mdate[this->num][i][j];
        }
    }

    //金币按钮背景图
    QPixmap pix(":/resource/BoardNode.png");
    QWidget * iconPane = new QWidget(this);
    iconPane->move(60,200);
    QGridLayout * layout = new QGridLayout();
    //清除布局空隙
    layout->setHorizontalSpacing(0);
    layout->setVerticalSpacing(0);
    layout->setContentsMargins(0,0,0,0);


    for(int i=0;i<4;i++){
        for (int j=0; j<4; j++) {
            QLabel * lb = new QLabel(iconPane);
            lb->setPixmap(pix);
            lb->setFixedSize(pix.width(),pix.height());

            layout->addWidget(lb,i,j,1,1);


            Coin * coin = new Coin(gameMap[i][j]);
            coin->setParent(lb);
            coin->move(lb->width()/2-coin->width()/2,lb->height()/2-coin->height()/2);
            //将每个coin对象存入数组
            coinArray[i][j] = coin;

            connect(coin, &Coin::clicked,this, [=](){
                flipSound->play();
                //阻塞所有硬币，不许点击
                for (int i=0; i<4; i++) {
                    for (int j=0; j<4; j++) {
                        coinArray[i][j]->Flipable = false;
                    }
                }

                //反转当前金币
                flipCoin(i,j);

                QTimer::singleShot(300,this, [=](){
                    //翻转上下左右金币
                    flipCoin(i+1,j);
                    flipCoin(i-1,j);
                    flipCoin(i,j+1);
                    flipCoin(i,j-1);

                    //解锁所有金币
                    for (int i=0; i<4; i++) {
                        for (int j=0; j<4; j++) {
                            coinArray[i][j]->Flipable = true;
                        }
                    }

                    win = true;
                    for (int i=0; i<4; i++) {
                        for (int j=0; j<4; j++) {
                            if(!(gameMap[i][j]==1)){
                                win = false;
                                break;
                            }
                        }
                    }

                    //判断胜利 需放在QTimer::singleShot里面
                    if(win){
                        winSound->play();
                        //锁定所有金币
                        for (int i=0; i<4; i++) {
                            for (int j=0; j<4; j++) {
                                coinArray[i][j]->Flipable = false;
                            }
                        }

                        //播放胜利动画
                        animation->start();
                    }
                });
            });
        }
    }

    iconPane->setLayout(layout);
}

void GameWidget::flipCoin(int x, int y){
    if((x>3)||(x<0)||(y<0)||(y>3)){
        return;
    }
    coinArray[x][y]->flip();
    gameMap[x][y] = gameMap[x][y]==0 ? 1:0;
}

void GameWidget::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/resource/PlayLevelSceneBg.png");
    painter.drawPixmap(0, 0, this->width(),this->height(),pix);

    pix.load(":/resource/Title.png");
    pix.scaled(pix.width()*1.7,pix.height()*1.7);
    painter.drawPixmap(this->width()*0.5-pix.width()*0.5,5,pix.width(),pix.height(),pix);

}
